#include <QIcon>

#include "stackedmainwindow.h"
#include "ui_stackedmainwindow.h"

StackedMainWindow::StackedMainWindow(QSettings *set, QWidget *parent) :
    QMainWindow(parent),
    m_ui(new Ui::StackedMainWindow), m_set(set)
{
    m_ui->setupUi(this);

    connect(m_ui->backBtn, SIGNAL(clicked()), this, SLOT(backward()));
}

StackedMainWindow::~StackedMainWindow()
{
    delete m_ui;
}

void StackedMainWindow::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void StackedMainWindow::closeEvent(QCloseEvent *e)
{
    QByteArray window = saveGeometry();

    m_set->setValue("stackedWindow", window);

    m_set->sync();

    e->accept();

    emit closed();
}

CTextBrowser* StackedMainWindow::textBrowser()
{
    return m_ui->textBrowser;
}

CTableView* StackedMainWindow::tableView()
{
    return m_ui->tableView;
}

CTreeView* StackedMainWindow::treeView()
{
    return m_ui->treeView;
}

void StackedMainWindow::show()
{
    if(m_set->value("stackedWindow").isValid())
        restoreGeometry(m_set->value("stackedWindow").toByteArray());

    QMainWindow::show();
}

void StackedMainWindow::showSections()
{
    m_ui->stackedWidget->setCurrentIndex(0);
    m_ui->backBtn->setIcon(QIcon(":/icons/cross"));
}

void StackedMainWindow::showEntries()
{
    m_ui->stackedWidget->setCurrentIndex(1);
    m_ui->backBtn->setIcon(QIcon(":/icons/arrow_left"));
}

void StackedMainWindow::showBrowser()
{
    m_ui->stackedWidget->setCurrentIndex(2);
}

void StackedMainWindow::backward()
{
    if(!m_ui->stackedWidget->currentIndex())
    {
        close();
        return;
    }

    m_ui->stackedWidget->setCurrentIndex(m_ui->stackedWidget->currentIndex()-1);
    if(!m_ui->stackedWidget->currentIndex())
    {
        m_ui->backBtn->setIcon(QIcon(":/icons/cross"));
    }

}
